package org.shock.ss.sms.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.fr.privilege.constants.PrivilegeConstants;

import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.system.PrivConstants;
import org.shock.ss.sms.commons.utils.ReflexUtils;

import org.shock.ss.sms.bean.Tree;
import org.shock.ss.sms.bean.system.Dept;
import org.shock.ss.sms.bean.system.Module;
import org.shock.ss.sms.bean.system.User;
import org.shock.ss.sms.bean.system.UserInfor;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.service.system.DeptService;
import org.shock.ss.sms.service.system.impl.DeptServiceImpl;
import org.shock.ss.sms.action.BaseAction;

public class DeptAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	/** 获得service */
	private DeptService ds = new DeptServiceImpl(DaoFactory.getDao("DeptDao"));
	private Dept dept = new Dept();
	private List list = new ArrayList();
	private Long node;
	private String treeString;
	private String xflxString;

	@RightAnno(rightNote = "部门管理")
	public String deptAdmin() throws Exception {
		return SUCCESS;
	}

	/** 生成部门树，用于extjs */
	public String genDeptTree() throws Exception {
		String sqladd = "";
		UserInfor userinfor = getUserInfor();
		// System.out.println(node + ":" + userinfor.getBmid());
		/* 如果不是系统超级管理员或者市局管理员，则只能看到本部门及以下单位 */
		if (userinfor.getRolecodes().contains("dwdack")) {
			// String sjbmbh = Tools.getString("select bmbh from
			// "+Dept.getTable()+" where
			// id=(select sjbmid from "+Dept.getTable()+" where id="
			// + userinfor.getBmid() + ")");
			Map map = new HashMap();
			if (node == 34000000) {
				map = DbTools
						.getMap("select bmbh,ifnull(lev,0)lev from " + Dept.getTable()
								+ " where id=(select sjbmid from " + Dept.getTable() + " where id="
								+ userinfor.getBmid() + ")");
				if (null == map) {
					sqladd += "and sjbmid=34000000";
				} else {
					sqladd += " and bmbh like '" + map.get("bmbh") + "%' and lev="
							+ (Integer.parseInt(map.get("lev") + ""));
				}
				node = null;
			}

		} else if (userinfor.getIsadmin() != 1 && !userinfor.getRolecodes().contains("xfadmin")
				&& !userinfor.getRolecodes().contains("qjdack")) {
			String bmbh = DbTools.getString("select bmbh from " + Dept.getTable() + " where id=" + userinfor.getBmid());
			// sqladd+=" and ('"+bmbh+"' like ''||bmbh||'%' or bmbh like
			// '"+bmbh+"%')";
			sqladd += " and ('" + bmbh + "' like ''||bmbh||'%' or bmbh like '" + bmbh + "%')";
			// System.out.println(node + ":" + userinfor.getBmid());
			node = (node == 34000000 ? userinfor.getBmid() : node);
		}
		if (userinfor.getRolecodes().contains("xueyuan") && userinfor.getRolecodes().size() == 1) {
			sqladd += " and id=" + userinfor.getBmid();
		}
		if (node != null) {
			sqladd += " and sjbmid=" + node;
		}
		sqladd += " and zt=0";
		log.debug(sqladd);
		List<Map> listMap = ds.listAll("select * from " + Dept.getTable() + " where 1=1" + sqladd
				+ " order by bmpx asc");
		// System.out.println("select * from "+Dept.getTable()+" where 1=1" +
		// sqladd + " order by bmpx asc");
		for (Map map : listMap) {
			Tree tree = new Tree(map.get("id") + "", (String) map.get("bmmc"),
					Integer.parseInt(map.get("leaf") + "") == 0 ? false : true, "bm", "", "");
			tree.setChecked(false);
			list.add(tree);
		}
		jsonString = JSON.toJSONString(list);
		log.debug(jsonString);
		return SUCCESS;
	}

	/** 生成部门用户树，用于extjs */
	public String genDeptUserTree() throws Exception {
		String sqladd = "";
		UserInfor userinfor = getUserInfor();
		// System.out.println(node + ":" + userinfor.getBmid());
		/* 如果不是系统超级管理员或者市局管理员，则只能看到本部门及以下单位 */
		if (userinfor.getRolecodes().contains("dwdack")) {
			// String sjbmbh = Tools.getString("select bmbh from
			// "+Dept.getTable()+" where
			// id=(select sjbmid from "+Dept.getTable()+" where id="
			// + userinfor.getBmid() + ")");
			Map map = new HashMap();
			if (node == 34000000) {
				map = DbTools
						.getMap("select bmbh,ifnull(lev,0)lev from " + Dept.getTable()
								+ " where id=(select sjbmid from " + Dept.getTable() + " where id="
								+ userinfor.getBmid() + ")");
				if (null == map) {
					sqladd += "and sjbmid=34000000";
				} else {
					sqladd += " and bmbh like '" + map.get("bmbh") + "%' and lev="
							+ (Integer.parseInt(map.get("lev") + ""));
				}
				node = null;
			}

		} else if (userinfor.getIsadmin() != 1 && !userinfor.getRolecodes().contains("xfadmin")
				&& !userinfor.getRolecodes().contains("qjdack")) {
			String bmbh = DbTools.getString("select bmbh from " + Dept.getTable() + " where id=" + userinfor.getBmid());
			// sqladd+=" and ('"+bmbh+"' like ''||bmbh||'%' or bmbh like
			// '"+bmbh+"%')";
			sqladd += " and ('" + bmbh + "' like ''||bmbh||'%' or bmbh like '" + bmbh + "%')";
			// System.out.println(node + ":" + userinfor.getBmid());
			node = (node == 34000000 ? userinfor.getBmid() : node);
		}
		if (userinfor.getRolecodes().contains("xueyuan") && userinfor.getRolecodes().size() == 1) {
			sqladd += " and id=" + userinfor.getBmid();
		}
		if (node != null) {
			sqladd += " and sjbmid=" + node;
		}
		sqladd += " and zt=0";
		log.debug(sqladd);
		List<Map> listMap = ds.listAll("select * from " + Dept.getTable() + " where 1=1" + sqladd
				+ " order by bmpx asc");
		// System.out.println("select * from "+Dept.getTable()+" where 1=1" +
		// sqladd + " order
		// by bmpx asc");
		for (Map map : listMap) {
			Tree tree = new Tree(map.get("id") + "", (String) map.get("bmmc"),
					Integer.parseInt(map.get("leaf") + "") == 0 ? false : true, "bm", "", "");
			tree.setChecked(false);
			list.add(tree);
		}
		jsonString = JSON.toJSONString(list);
		log.debug(jsonString);
		return SUCCESS;
	}

	/** 显示部门信息 */
	public String viewDept() throws Exception {
		String sql = "select *from " + Dept.getTable() + " where id=" + dept.getId();
		log.debug(sql);
		Map map = ds.view(sql);
		log.debug(map);
		jsonString = JSONObject.fromObject(map).toString();
		log.debug(jsonString);
		return SUCCESS;
	}

	/** 更新部门信息 */
	public String updateDept() throws Exception {
		ds.update(dept);
		return SUCCESS;
	}

	/** 添加部门信息 */
	public String addDept() throws Exception {
		log.info(ReflexUtils.rwritertoString(dept));// 调试bean信息
		if (action.equals("before")) {// 显示添加页面
			list = ds.listAll("select id,name from " + Dept.getTable() + " order by bmpx");
			log.debug(list);
			return "before";
		}
		ds.add(dept);
		return SUCCESS;
	}

	/** 删除部门信息 */
	public String delDept() throws Exception {
		List<Long> idsList = new ArrayList<Long>();
		idsList.add(dept.getId());
		String ids = DbTools.getSubIdsStr(idsList, Dept.getTable(), "pid", dept.getId() + "");
		String sql = "delete from " + Dept.getTable() + " where id in (" + ids + ")";
		log.debug(sql);
		ds.del(sql);
		return SUCCESS;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Long getNode() {
		return node;
	}

	public void setNode(Long node) {
		this.node = node;
	}

	public String getTreeString() {
		return treeString;
	}

	/**
	 * @return the xflxString
	 */
	public String getXflxString() {
		return xflxString;
	}

	/**
	 * @param xflxString
	 *            the xflxString to set
	 */
	public void setXflxString(String xflxString) {
		this.xflxString = xflxString;
	}

}
